package com.s59.rbac.service.impl;

import com.s59.rbac.entity.Menu;
import com.s59.rbac.entity.Role;
import com.s59.rbac.service.IMenuService;
import com.s59.rbac.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class PermssionService {

    @Autowired
    private IRoleService roleService;
    @Autowired
    private IMenuService menuService;

    public Set<String> getRoleKeys(String userName){
        return roleService.getByUserName(userName).stream().map(Role::getRoleKey).collect(Collectors.toSet());
    }

    public Set<String> getPerms(String userName){
        List<Role> roleList = roleService.getByUserName(userName);
        List<Menu> menuList = menuService.getByRoleIdList(roleList.stream().mapToLong(Role::getRoleId).boxed().collect(Collectors.toList()));
        return menuList.stream()
                .filter(item -> StringUtils.hasText(item.getPerms()))
                .map(Menu::getPerms).collect(Collectors.toSet());
    }
}
